package kara;


import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

//bfs
public class A99P2 {
    static int n;
    static int m;
    static int[][] map;
    static int[] dir1={1,0,-1,0};
    static int[] dir2={0,1,0,-1};
 static   class Pair{
        int x;
        int y;
        Pair(int x,int y){
            this.x=x;
            this.y=y;
        }
    }
    static   BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
    static   BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        int res=0;
        String[] s = read.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m=Integer.parseInt(s[1]);
        map=new int[n][m];
        for(int i=0;i<n;i++){
            String[] h = read.readLine().split(" ");
            for(int j=0;j<m;j++){
                map[i][j]=Integer.parseInt(h[j]);
            }
        }
//深搜
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(map[i][j]==1){
//标记为海洋
                    //搜索

                    bfs(i,j);
                    res++;
                }
            }
        }
        log.write(Integer.toString(res));
        log.flush();
        read.close();
        log.close();
    }

   static void bfs(int x, int y){

        Queue<Pair> queue=new LinkedList<>();
        //加入队列
        queue.offer(new Pair(x,y));
        //标记为搜索过
       map[x][y]=0;//变为海洋
        while(!queue.isEmpty()){
            Pair poll = queue.poll();
            int a=poll.x;
            int b=poll.y;
            for(int i=0;i<4;i++){
                //将合法的陆地标记为海洋并且加进来
                int ee=a+dir1[i];
                int rr=b+dir2[i];
                if(ee>=0&&ee<n&&rr>=0&&rr<m&&map[ee][rr]==1){
                    //标记
                    map[ee][rr]=0;
                    queue.offer(new Pair(ee,rr));
                }
            }
        }
    }
}
